package com.yipai.datewidget.booking.page;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;

import com.lxj.xpopup.util.KeyboardUtils;
import com.yipai.datewidget.booking.BookingSharedViewModel;
import com.yipai.datewidget.booking.R;
import com.yipai.datewidget.booking.base.BaseFragment;
import com.yipai.datewidget.booking.config.Config;
import com.yipai.datewidget.booking.databinding.FragmentRemarksBinding;

public class RemarksFragment extends BaseFragment {

    public static RemarksFragment newInstance(int index) {
        Bundle args = new Bundle();
        RemarksFragment fragment = new RemarksFragment();
        args.putInt(Config.KEY_INDEX, index);
        fragment.setArguments(args);
        return fragment;
    }

    private int mPageIndex;
    private BookingSharedViewModel mBookingSharedViewModel;
    private FragmentRemarksBinding mFragmentRemarksBinding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mFragmentRemarksBinding = FragmentRemarksBinding.inflate(inflater, container, true);
        mPageIndex = getArguments().getInt(Config.KEY_INDEX);
        mBookingSharedViewModel = getActivityScopeViewModel(BookingSharedViewModel.class);
        initUi();
        initObserver();
        return mFragmentRemarksBinding.getRoot();
    }

    private void initObserver() {
        mBookingSharedViewModel.mRemarksMutableLiveData.observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(String s) {
                if (!TextUtils.isEmpty(s)) {
                    mFragmentRemarksBinding.etContent.setText(s);
                }
            }
        });
    }

    private void initUi() {
        mFragmentRemarksBinding.tvContentNum.setText(getString(R.string.remarks_content_limit, String.valueOf(0)));
        mFragmentRemarksBinding.etContent.setHorizontallyScrolling(false);

        mFragmentRemarksBinding.etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String text = mFragmentRemarksBinding.etContent.getText().toString();
                mFragmentRemarksBinding.tvContentNum.setText(getString(R.string.remarks_content_limit, String.valueOf(text.length())));
            }
        });

        mFragmentRemarksBinding.tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = mFragmentRemarksBinding.etContent.getText().toString();
                mBookingSharedViewModel.mRemarksMutableLiveData.setValue(text);
                mBookingSharedViewModel.mNextStepUnPeekLiveData.setValue(mPageIndex);
            }
        });

        mFragmentRemarksBinding.etContent.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if(!b){
                    KeyboardUtils.hideSoftInput(mFragmentRemarksBinding.etContent);
                }
            }
        });
    }
}
